<?php

Globals::requireClass('Controller');
Globals::requireTable('Delivery');
Globals::requireTable('DeliveryPoint');
Globals::requireTable('Custom');
Globals::requireTable('Custom_contact');
Globals::requireTable('Custom_site');
Globals::requireTable('Custom_site_contact');
Globals::requireTable('City');
Globals::requireTable('Province');

class CustomerController extends Controller {

    protected $DeliveryTable;
    protected $DeliveryPointTable;
    protected $CustomTable;
    protected $Custom_contactTable;
    protected $Custom_siteTable;
    protected $Custom_site_contactTable;
    protected $CityTable;
    protected $ProvinceTable;
    public static $defaultConfig = array(
        'viewEnabled' => true,
        'layoutEnabled' => true,
        'title' => null
    );

    public function __construct($config = null) {
        parent::__construct($config);
        $this->DeliveryTable = new DeliveryTable($config);
        $this->DeliveryPointTable = new DeliveryPointTable($config);
        $this->CustomTable = new CustomTable($config);
        $this->Custom_contactTable = new Custom_contactTable($config);
        $this->Custom_siteTable = new Custom_siteTable($config);
        $this->Custom_site_contactTable = new Custom_site_contactTable($config);
        $this->CityTable = new CityTable($config);
        $this->ProvinceTable = new ProvinceTable($config);
    }

    public function listAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        if (!in_array(9, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $nickname = $this->getParam('nickname');
        $status = $this->getParam('search_type');
        $row_val = $this->getParam('row_val');
        $this->view->row_val = $row_val ? $row_val : 2;
        $where = "user_id = '{$uid}'";
        $where2 = "user_id = '{$uid}'";

        if (!empty($nickname)) {
            $where .= " and nickname like '%{$nickname}%'";
            $this->view->nickname = $nickname;
        }

        if (!empty($status)) {
            if ($status == 3) {
                $where .= " and status > 0";
                $where2 .= " and status > 0";
                $this->view->status = $status;
            } else {
                $where .= " and status = '{$status}'";
                $where2 .= " and status = '{$status}'";
                $this->view->status = $status;
            }
        } else {
            $where .= " and status > 0";
            $where2 .= " and status > 0";
        }

        $re = $this->CustomTable->listall($where);
        $count = $this->Custom_contactTable->listCount($where2);
                $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $re2 = $this->Custom_contactTable->listPage($where, "id desc", $pageSize, $pageId);
//        $re2 = $this->Custom_contactTable->listall($where2);

        foreach ($re2 as $k => $v) {
            $re2[$k]['custom_name'] = '';
            foreach ($re as $kk => $vv) {
                if ($v['custom_id'] == $vv['id']) {
                    $re2[$k]['custom_name'] = $re[$kk]['name'];
                }
            }
        }

        $this->view->data = $re;
        $this->view->data2 = $re2;

        $this->config['layoutEnabled'] = false;
    }

    public function docustomaddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $nickname = $this->getParam('nickname');
        $code = $this->getParam('code');
        $name = $this->getParam('name');
        $enname = $this->getParam('enname');
        $tax = $this->getParam('tax');
        $is_same = $this->getParam('is_same');
        $remind = $this->getParam('remind');
        $remind_people = $this->getParam('remind_people');
        $debt_max = $this->getParam('debt_max');
        $back_limit = $this->getParam('back_limit');
        $address = $this->getParam('address');
        $remark = $this->getParam('remark');

        $con_name = $this->getParam('con_name');
        $telephone = $this->getParam('telephone');
        $cellphone = $this->getParam('cellphone');
        $fax = $this->getParam('fax');
        $email = $this->getParam('email');
        $qq = $this->getParam('qq');

        $time = date("Y-m-d H:i:s", time());

        $where = "user_id = '{$uid}'";
        $re = $this->CustomTable->listall($where);
        foreach ($re as $k => $v) {
            if ($v['name'] == $name) {
                echo 0;
                exit;
            }
        }

        $cus_data = array(
            '`user_id`' => $uid,
            '`nickname`' => $nickname,
            '`code`' => $code,
            '`name`' => $name,
            '`enname`' => $enname,
            '`tax`' => $tax,
            '`is_same`' => $is_same,
            '`remind`' => $remind,
            '`remind_people`' => $remind_people,
            '`debt_max`' => $debt_max,
            '`back_limit`' => $back_limit,
            '`address`' => $address,
            '`remark`' => $remark,
            '`createtime`' => $time,
        );
        $custom_id = $this->CustomTable->add($cus_data, true);

        $con_data = array(
            '`user_id`' => $uid,
            '`custom_id`' => $custom_id,
            '`name`' => $con_name,
            '`telephone`' => $telephone,
            '`cellphone`' => $cellphone,
            '`fax`' => $fax,
            '`email`' => $email,
            '`qq`' => $qq,
            '`createtime`' => $time,
        );

        $re = $this->Custom_contactTable->add($con_data);
        echo $re;
        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function docustomupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $nickname = $this->getParam('nickname');
        $code = $this->getParam('code');
        $name = $this->getParam('name');
        $enname = $this->getParam('enname');
        $tax = $this->getParam('tax');
        $is_same = $this->getParam('is_same');
        $remind = $this->getParam('remind');
        $remind_people = $this->getParam('remind_people');
        $debt_max = $this->getParam('debt_max');
        $back_limit = $this->getParam('back_limit');
        $address = $this->getParam('address');
        $remark = $this->getParam('remark');

        $id = $this->getParam('id');


        $where = "id =" . $id;
        $cus_data = array(
            '`nickname`' => $nickname,
            '`code`' => $code,
            '`name`' => $name,
            '`enname`' => $enname,
            '`tax`' => $tax,
            '`is_same`' => $is_same,
            '`remind`' => $remind,
            '`remind_people`' => $remind_people,
            '`debt_max`' => $debt_max,
            '`back_limit`' => $back_limit,
            '`address`' => $address,
            '`remark`' => $remark,
        );
        $re = $this->CustomTable->update($cus_data, $where);

        echo $re;
        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function docustom_conupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }

        $con_name = $this->getParam('con_name');
        $telephone = $this->getParam('telephone');
        $cellphone = $this->getParam('cellphone');
        $fax = $this->getParam('fax');
        $email = $this->getParam('email');
        $qq = $this->getParam('qq');

        $id = $this->getParam('id');


        $where = "id =" . $id;

        $con_data = array(
            '`name`' => $con_name,
            '`telephone`' => $telephone,
            '`cellphone`' => $cellphone,
            '`fax`' => $fax,
            '`email`' => $email,
            '`qq`' => $qq,
        );

        $re = $this->Custom_contactTable->update($con_data, $where);
        echo $re;
        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function docustomdelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->CustomTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function docustom_condelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->Custom_contactTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function sitelistAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        if (!in_array(10, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $cus_name = $this->getParam('cus_name');
        $nickname = $this->getParam('nickname');
        $province_id = $this->getParam('province_id');
        $city = $this->getParam('city');
        $status = $this->getParam('search_type');
        $row_val = $this->getParam('row_val');
        $this->view->row_val = $row_val ? $row_val : 2;
        $where = "user_id = '{$uid}'";
        $where2 = "user_id = '{$uid}'";

        if (!empty($cus_name)) {
            $cus_where = " name like '%{$cus_name}%'";
            $cus_id = $this->CustomTable->listall($cus_where);
            if ($cus_id != array()) {
                $where .= " and custom_id = '{$cus_id[0]['id']}'";
            }
            $this->view->cus_name = $cus_name;
        }

        if (!empty($nickname)) {
            $where .= " and nickname like '%{$nickname}%'";
            $this->view->nickname = $nickname;
        }

        if (!empty($province_id)) {
            $where .= " and province_id like '%{$province_id}%'";
            $this->view->province_id = $province_id;
        }

        if (!empty($city)) {
            $where .= " and city like '%{$city}%'";
            $this->view->city = $city;
        }


        if (!empty($status)) {
            if ($status == 3) {
                $where .= " and status > 0";
                $where2 .= " and status > 0";
                $this->view->status = $status;
            } else {
                $where .= " and status = '{$status}'";
                $where2 .= " and status = '{$status}'";
                $this->view->status = $status;
            }
        } else {
            $where .= " and status > 0";
            $where2 .= " and status > 0";
        }
      $count = $this->Custom_site_contactTable->listCount($where);
        //$re1 = $this->Custom_site_contactTable->listall($where2);
       $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $re1 = $this->Custom_site_contactTable->listPage($where, "id desc", $pageSize, $pageId);

        $re2 = $this->CustomTable->listall($where2);

        $re3 = $this->CityTable->listall($where2);

        $re4 = $this->ProvinceTable->listall($where2);

        $re = $this->Custom_siteTable->listall($where);
        foreach ($re as $k => $v) {
            $re[$k]['custom_name'] = '';
            foreach ($re2 as $kk => $vv) {
                if ($v['custom_id'] == $vv['id']) {
                    $re[$k]['custom_name'] = $re2[$kk]['name'];
                }
            }
        }

        foreach ($re as $k => $v) {
            $re[$k]['city_name'] = '';
            foreach ($re3 as $kk => $vv) {
                if ($v['city'] == $vv['id']) {
                    $re[$k]['city_name'] = $re3[$kk]['name'];
                }
            }
        }

        foreach ($re as $k => $v) {
            $re[$k]['province_name'] = '';
            foreach ($re4 as $kk => $vv) {
                if ($v['province_id'] == $vv['id']) {
                    $re[$k]['province_name'] = $re4[$kk]['name'];
                }
            }
        }

        foreach ($re1 as $k => $v) {
            $re1[$k]['custom_site_name'] = '';
            foreach ($re as $kk => $vv) {
                if ($v['custom_site_id'] == $vv['id']) {
                    $re1[$k]['custom_site_name'] = $re[$kk]['name'];
                }
            }
        }

        $this->view->data = $re;
        $this->view->data1 = $re1;
        $this->view->custom_data = $re2;
        $this->view->city_data = $re3;
        $this->view->province_data = $re4;

        $this->config['layoutEnabled'] = false;
    }

    public function dositeaddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $nickname = $this->getParam('nickname');
        $code = $this->getParam('code');
        $name = $this->getParam('name');
        $custom_id = $this->getParam('custom_id');
        $province_id = $this->getParam('province_id');
        $city = $this->getParam('city');
        $goods_name = $this->getParam('goods_name');
        $address = $this->getParam('address');
        $remark = $this->getParam('remark');

        $con_name = $this->getParam('con_name');
        $telephone = $this->getParam('telephone');
        $cellphone = $this->getParam('cellphone');
        $fax = $this->getParam('fax');
        $email = $this->getParam('email');
        $qq = $this->getParam('qq');

        $time = date("Y-m-d H:i:s", time());

        $where = "user_id = '{$uid}'";
        $re = $this->Custom_siteTable->listall($where);
        foreach ($re as $k => $v) {
            if ($v['name'] == $name) {
                echo 0;
                exit;
            }
        }

        $cus_data = array(
            '`user_id`' => $uid,
            '`nickname`' => $nickname,
            '`code`' => $code,
            '`name`' => $name,
            '`custom_id`' => $custom_id,
            '`province_id`' => $province_id,
            '`city`' => $city,
            '`goods_name`' => $goods_name,
            '`address`' => $address,
            '`remark`' => $remark,
            '`createtime`' => $time,
        );
        $custom_site_id = $this->Custom_siteTable->add($cus_data, true);

        if ($custom_site_id < 1) {
            echo 0;
            exit;
        }
        $con_data = array(
            '`user_id`' => $uid,
            '`custom_id`' => $custom_id,
            '`custom_site_id`' => $custom_site_id,
            '`name`' => $con_name,
            '`telephone`' => $telephone,
            '`cellphone`' => $cellphone,
            '`fax`' => $fax,
            '`email`' => $email,
            '`qq`' => $qq,
            '`createtime`' => $time,
        );

        $re = $this->Custom_site_contactTable->add($con_data);
        echo $re;
        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function dositeupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $nickname = $this->getParam('nickname');
        $code = $this->getParam('code');
        $name = $this->getParam('name');
        $custom_id = $this->getParam('custom_id');
        $city = $this->getParam('city');
        $goods_name = $this->getParam('goods_name');
        $address = $this->getParam('address');
        $remark = $this->getParam('remark');

        $id = $this->getParam('id');

        $where = " id =" . $id;
        $cus_data = array(
            '`nickname`' => $nickname,
            '`code`' => $code,
            '`name`' => $name,
            '`custom_id`' => $custom_id,
            '`city`' => $city,
            '`goods_name`' => $goods_name,
            '`address`' => $address,
            '`remark`' => $remark,
        );
        $re = $this->Custom_siteTable->update($cus_data, $where);

        echo $re;
        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function dosite_conupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $con_name = $this->getParam('con_name');
        $telephone = $this->getParam('telephone');
        $cellphone = $this->getParam('cellphone');
        $fax = $this->getParam('fax');
        $email = $this->getParam('email');
        $qq = $this->getParam('qq');

        $id = $this->getParam('id');

        $where = " id =" . $id;
        $con_data = array(
            '`name`' => $con_name,
            '`telephone`' => $telephone,
            '`cellphone`' => $cellphone,
            '`fax`' => $fax,
            '`email`' => $email,
            '`qq`' => $qq,
        );

        $re = $this->Custom_site_contactTable->update($con_data, $where);
        echo $re;
        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function dositedelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->Custom_siteTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function dosite_condelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->Custom_site_contactTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    protected function out() {
        $this->layout->nav = 'index';
        parent::out();
    }

}

Config::extend('CustomerController', 'Controller');
